#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"


__all__ = [u'install_translator', u'MO_FILE_DIR', u'tr_',
           u'hardbook', u'defaultconf']

global tr_

import os
import sys
import gettext

tr_ = gettext.NullTranslations().ugettext
try :
  __builtins__['tr_'] = tr_
except TypeError :
  __builtins__.tr_ = tr_

TRANSLATORS = {}
__CURRENT_LOCALE = None

try :
  __file__
except NameError :
  co = sys._getframe(0).f_code
  __file__ = co.co_filename

def install_translator(locale):
  global __CURRENT_LOCALE, tr_
  if locale == __CURRENT_LOCALE :
    return
  __CURRENT_LOCALE = locale
  try :
    tr = TRANSLATORS[locale]
  except KeyError :
    tr = gettext.NullTranslations()
  tr.install(unicode=True)
  tr_ = _
  __builtins__['tr_'] = _


try :
  from _pylot.hardbookdef import (hardbook, defaultconf)
except ImportError :
  from _pylot.setuptools_hardbookdef import (hardbook, defaultconf)

from _pylot.data.regexpr import match, co_i18n_locale


MO_FILE_DIR = hardbook(u'i18n_gettext')
NAME = 'pylot'

if MO_FILE_DIR.exists() :
  for path in MO_FILE_DIR.listdir() :
    if os.path.isdir(path) and match(co_i18n_locale, (os.path.basename(path))) :
      locale = os.path.basename(path)
      TRANSLATORS[locale] = gettext.translation(NAME, MO_FILE_DIR,
        languages=[locale], fallback=True)
